package com.example.ddz.framework.impl;

/**
 * Created by IntelliJ IDEA.
 * User: Marcel Melzig
 * Date: 26.07.11
 * Time: 18:45
 */

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

/**
 * This class handlers the functions of the accelerometer.
 */
public class AccelerometerHandler implements SensorEventListener
{
    private float accelX;
    private float accelY;
    private float accelZ;

    /**
     * Creates an instance of the AccelerometerHandler
     *
     * @param context The context of this application.
     */
    public AccelerometerHandler(Context context)
    {
        SensorManager manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0)
        {
            Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
            manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
        }

    }

    /**
     * This event must be implemented but is not used in the Framework jet.
     *
     * @param sensor   The sensor.
     * @param accuracy The accuracy.
     */
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy)
    {
        try
        {
            throw new Exception("Not implemented.");
        }
        catch (Exception e)
        {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    /**
     * Stores the coordinates of teh current position into local variables.
     *
     * @param event The event object of the event.
     */
    @Override
    public void onSensorChanged(SensorEvent event)
    {
        accelX = event.values[0];
        accelY = event.values[1];
        accelZ = event.values[2];
    }

    /**
     * Returns the x acceleration.
     *
     * @return The x acceleration.
     */
    public float GetAccelX()
    {
        return accelX;
    }

    /**
     * Returns the y acceleration.
     *
     * @return The y acceleration.
     */
    public float GetAccelY()
    {
        return accelY;
    }

    /**
     * Returns the z acceleration.
     *
     * @return The z acceleration.
     */
    public float GetAccelZ()
    {
        return accelZ;
    }
}
